


################
#Table B-1
################

reg_maker_vm <- function(which_model="moderates",who="Republicans",model_type="linear"){
  
  grouping <- (who=="Democrats")*100 + (who=="Republicans")*200
  
  if (which_model=="moderates" & model_type=="linear"){
    m <- lm(responsiveness_to_party_calls ~ cdist + majority +
              baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
              freshman  + best_committee + power_committee + chair + factor(congress), 
            data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  if (which_model=="moderates" & model_type=="mixed"){
    m <- lmer(responsiveness_to_party_calls ~ cdist + majority +
                baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
                freshman  + best_committee + power_committee + chair + (1|icpsr) + (1|congress), 
              data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  if (which_model=="extremists" & model_type=="linear"){
    m <- lm(responsiveness_to_party_calls ~ ideological_extremism_new + majority +
              baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
              freshman  + best_committee + power_committee + chair + factor(congress), 
            data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  if (which_model=="extremists" & model_type=="mixed"){
    m <- lmer(responsiveness_to_party_calls ~ ideological_extremism_new + majority +
                baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
                freshman  + best_committee + power_committee + chair + (1|icpsr) + (1|congress), 
              data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  if (which_model=="parties" & model_type=="linear"){
    m <- lm(responsiveness_to_party_calls ~ pdist + majority +
              baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
              freshman  + best_committee + power_committee + chair + factor(congress), 
            data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  if (which_model=="parties" & model_type=="mixed"){
    m <- lmer(responsiveness_to_party_calls ~ pdist + majority +
                baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
                freshman  + best_committee + power_committee + chair + (1|icpsr) + (1|congress), 
              data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  return(m)
}

reg_maker_me<- function(which_model="moderates",who="Republicans",model_type="linear"){
  
  grouping <- (who=="Democrats")*100 + (who=="Republicans")*200
  
  if (which_model=="moderates" & model_type=="linear"){
    m <- lm(loyalty ~ cdist + majority +
              baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
              freshman  + best_committee + power_committee + chair + factor(congress), 
            data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  if (which_model=="moderates" & model_type=="mixed"){
    m <- lmer(loyalty ~ cdist + majority +
                baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
                freshman  + best_committee + power_committee + chair + (1|icpsr) + (1|congress), 
              data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  if (which_model=="extremists" & model_type=="linear"){
    m <- lm(loyalty ~ ideological_extremism_new + majority +
              baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
              freshman  + best_committee + power_committee + chair + factor(congress), 
            data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  if (which_model=="extremists" & model_type=="mixed"){
    m <- lmer(loyalty ~ ideological_extremism_new + majority +
                baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
                freshman  + best_committee + power_committee + chair + (1|icpsr) + (1|congress), 
              data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  
  if (which_model=="parties" & model_type=="linear"){
    m <- lm(loyalty ~ pdist + majority +
              baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
              freshman  + best_committee + power_committee + chair + factor(congress), 
            data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  if (which_model=="parties" & model_type=="mixed"){
    m <- lmer(loyalty ~ pdist + majority +
                baseline_rate + dpres + south + votepct + female + african_american + latino + seniority + 
                freshman  + best_committee + power_committee + chair + (1|icpsr) + (1|congress), 
              data=subset(all_together5, party_code==grouping & leader==0))
  }
  
  return(m)
}

##print the table
fileConn<-file( paste0(getwd(), "/TableB1.txt"))
writeLines(
  (screenreg(list(reg_maker_vm(which_model = "extremists", who = "Republicans", model_type = "linear"),
                       reg_maker_me(which_model = "extremists", who = "Republicans", model_type = "linear"),
                       #reg_maker_vm(which_model = "extremists", who = "Republicans", model_type = "linear"),
                       #reg_maker_me(which_model = "extremists", who = "Republicans", model_type = "linear"),
                       #reg_maker_vm(which_model = "parties", who = "Republicans", model_type = "linear"),
                       #reg_maker_me(which_model = "parties", who = "Republicans", model_type = "linear"),
                       #reg_maker_vm(which_model = "moderates", who = "Democrats", model_type = "linear"),
                       #reg_maker_me(which_model = "moderates", who = "Democrats", model_type = "linear"),
                       reg_maker_vm(which_model = "extremists", who = "Democrats", model_type = "linear"),
                       reg_maker_me(which_model = "extremists", who = "Democrats", model_type = "linear")),
                  #reg_maker_vm(which_model = "parties", who = "Democrats", model_type = "linear")),
                  #reg_maker_me(which_model = "parties", who = "Democrats", model_type = "linear")),
                  omit.coef = "factor", 
                  custom.coef.names = c("Intercept","Extremism","Majority","Baseline Loyalty",
                                        "Dem. Pres. Vote", "South", "Voteshare", "Female",
                                        "Black", "Latino", "Seniority", "Freshman",
                                        "Best Cmte", "Power Cmte", "Cmte Chair"),
                  stars = 0.05)), 
  fileConn)
close(fileConn)

################
#Table C-1
################


mod_new <- lm(inc_vote ~ loyalty * cdist +
                qual_chal + presparty  + freshman + income_change + inc_spending_adv +
                inc_vote_lag + midterm + pres_pty_approval + factor(elyr) + presvote,
              data=subset(all_together4), x=TRUE)

mod_pu <- lm(inc_vote ~ party_unity * cdist +
               qual_chal + presparty  + freshman + income_change + inc_spending_adv +
               inc_vote_lag + midterm + pres_pty_approval  + factor(elyr) + presvote,
             data=subset(all_together4), x=TRUE)

mod_newWin <- glm(I(inc_vote>50) ~ loyalty * cdist +
                    qual_chal + presparty  + freshman + income_change + inc_spending_adv +
                    inc_vote_lag + midterm + pres_pty_approval + factor(elyr) + presvote,
                  data=subset(all_together4), x=TRUE, family = binomial)

mod_puWin <- glm(I(inc_vote>50) ~ party_unity * cdist +
                   qual_chal + presparty  + freshman + income_change + inc_spending_adv +
                   inc_vote_lag + midterm + pres_pty_approval  + factor(elyr) + presvote,
                 data=subset(all_together4), x=TRUE, family = binomial)

mod_new_close <- lm(inc_vote ~ loyalty * cdist +
                      qual_chal + presparty  + freshman + income_change + inc_spending_adv +
                      midterm + pres_pty_approval + factor(elyr) + presvote,
                    data=subset(all_together4, inc_vote_lag>=45 & inc_vote_lag<=55), x=TRUE)

mod_pu_close <- lm(inc_vote ~ party_unity * cdist +
                     qual_chal + presparty  + freshman + income_change + inc_spending_adv +
                     midterm + pres_pty_approval  + factor(elyr) + presvote,
                   data=subset(all_together4, inc_vote_lag>=45 & inc_vote_lag<=55), x=TRUE)


##print the table - NOTE THE ORDER OF COEFFICIENTS IS MIXED. PLEASE VERIFY LINE BY LINE.


fileConn<-file( paste0(getwd(), "/TableC1.txt"))
writeLines(
  screenreg(list(mod_new, mod_pu, mod_new_close, mod_pu_close), omit.coef = "factor",
            custom.coef.names = c("Intercept", "IRT Loyalty", "Distance from Chamber Median",
                                  "Quality Challenger", "President's Party", "Freshman",
                                  "Income Change", "Incumbent Spending Advantage",
                                  "Incumbent Voteshare (lagged)", "Midterm", "Presidential Approval",
                                  "Presidential Voteshare", "Loyalty times Distance",
                                  "Party Unity", "Unity times Distance"), stars = 0.05)
  , 
  fileConn)
close(fileConn)

################
#Table D-1
################

load(paste0(getwd(), "/EMestimates.Rda"))
load(paste0(getwd(), "/EMestimates_boot.Rda"))
load(paste0(getwd(), "/EMestimates_placebo.Rda"))
load("speeches.Rda")
load("y.Rda")
load("all_data.Rda")

##use bootstrap data to calculate uncertainties
results_boot[results_boot==0] <- NA
bmeans <- colMeans(results_boot,na.rm=TRUE)
blow <- apply(results_boot,2,quantile,0.05,na.rm=TRUE)
bhigh <- apply(results_boot,2,quantile,0.95,na.rm=TRUE)

N <- length(unique(all_data$id))

full_results <- data.frame(id=rep(as.numeric(rownames(y)),13), cong=rep(101:113,each=N),
                           ideals=rep(bmeans[1:N],13), ideal_low=rep(blow[1:N],13), ideal_high=rep(bhigh[1:N],13),
                           loyalty=bmeans[(N+1):length(bmeans)],
                           loyalty_lag=c(rep(NA,N),bmeans[(N+1):(length(bmeans)-N)]),
                           loyalty_low=blow[(N+1):length(blow)],
                           loyalty_high=bhigh[(N+1):length(bhigh)]
)
##compare results

results_placebo$cong <- rep(101:113,each=1293)

res_sum <-
  results_placebo %>%
  dplyr::group_by(id,cong) %>%
  dplyr::summarise(loyalty_placebo = mean(delta,na.rm.=TRUE),
                   loyalty_placebo_low = quantile(delta, 0.05, na.rm = TRUE), loyalty_placebo_high = quantile(delta, 0.95, na.rm = TRUE)) %>%
  ungroup()

res_sum$loyalty_placebo[res_sum$loyalty_placebo==0] <- NA

res_sum_together <- merge(res_sum, full_results, by = c("id","cong"))

res_sum_together <- subset(res_sum_together, loyalty != 0 & loyalty_placebo !=0)

res_sum_together$sig_loyalty_placebo <- "Not Significant"
res_sum_together$sig_loyalty_placebo[sign(res_sum_together$loyalty_placebo_low*res_sum_together$loyalty_placebo_high) == 1] <- "Significant"
res_sum_together$sig_loyalty <- "Not Significant"
res_sum_together$sig_loyalty[sign(res_sum_together$loyalty_low*res_sum_together$loyalty_high) == 1] <- "Significant"



##print the table

fileConn<-file( paste0(getwd(), "/TableD1.txt"))
writeLines(
  print(xtable(table(res_sum_together$sig_loyalty, res_sum_together$sig_loyalty_placebo))), 
  fileConn)
close(fileConn)



################
#Table E-1
################

sum_tableMaker <- function(){
  cat(paste(round(100*comp_tab_base,digits=2),round(sum(log_lik_base,na.rm=TRUE)),
            round(AIC_base),round(100*GMP_total_base,digits=2),round(100*APRE_base_tot,digits=2),sep=" & "), "\n",
      paste(round(100*comp_tab,digits=2),round(sum(log_lik_new,na.rm=TRUE)),
            round(AIC_new),round(100*GMP_total_new,digits=2),round(100*APRE_new_tot,digits=2),sep=" & "))
}

##print the table

sink( paste0(getwd(), "/TableE1.txt"))
sum_tableMaker()
sink()

cat("Replication file 7 done.","\n")

